##################################
#-*- coding:utf8 -*-             #
import urllib
from sgmllib import SGMLParser
import  xml.dom
import  libxml2dom
from    libxmlmods   import libxml2mod
from    xml.dom      import Node

def nodeInfo(n):
    print 'nodetype%d''n.tageName=%d'%(n.nodeType,n.tageName)
def NodeText(node,skipset,textSet):
    text=u""
 #   skipset=('a','cite')
    if node.localName in skipset:
        return u''
    try:
        for s in node.childNodes:
            text=text+NodeText(s,skipset,textSet)
    except Exception,ex:        
        print Exception,':',ex
        pass
     
    try:
#        textSet=('text','em','b')
        if node.localName in textSet: 
            text=node.textContent
        elif node.localName=='cite':
            print node.textContent    
    except:
        pass
     
    return text     
def element_h3(h3_node):
##    print h3_node.nodeType
##    print h3_node.tagName
##    print h3_node.getAttribute('class')
##    print 'h3 first child',h3_node.firstChild
##    print h3_node.firstChild.getAttribute('href')
 
    if h3_node.tagName=='h3'and h3_node.getAttribute('class')=='r':
        a=h3_node.firstChild
        url=a.getAttribute('href')
        urltxt=a.firstChild.data        
    else:
        raise
    
    divN=h3_node.nextSibling
      
    if divN.nodeType==Node.ELEMENT_NODE and divN.nodeName=='div':                
          skipset=('a','cite')
          textSet=('text','em','b')
          text=NodeText(divN,skipset,textSet)
          cite=NodeText(divN,(),('cite'))          
#         print text        
    else:
        raise        
    
    return {'href':url,'hreftxt':urltxt,'text':text,'cite':cite}

def parser(download_file,element_handle):
#    print Node.ELEMENT_NODE
#    , node.ATTRIBUTE_NODE, node.TEXT_NODE, node.CDATA_SECTION_NODE, node.ENTITY_NODE, node.PROCESSING_INSTRUCTION_NODE, node.COMMENT_NODE, DOCUMENT_NODE, node.DOCUMENT_TYPE_NODE, NOTATION_NODE
    html=open(download_file)
    injected=0
    doc = libxml2dom.parseString(html.read(), html=1)
    
    tables = doc.getElementsByTagName("table") 
#    print tables
    lis = doc.getElementsByTagName("li")
 #   print lis
    i=0
    j=0
    for li in lis:
        try:
            first=li.firstChild
            print '[%d]---------'%i
            element=element_h3(first)
            element_handle(element)    
        except Exception,ex:        
#            print Exception,':',ex                   
            pass